from PySide6.QtCore import Qt, QTime, Slot
from PySide6.QtWidgets import QDialog, QMessageBox

from Devices.Chiot.Bulb.DeviceBulb import DeviceBulb
from Devices.Chiot.Bulb.BulbParser import BulbParser
from Components.MessageBoxFunc import MessageBoxFunc

from Devices.Chiot.Bulb.Forms.TimerSetWindow_UI import Ui_FormTimerSet

class TimerSetWindow(QDialog):
    def __init__(self, parent = None):
        super().__init__(parent)
        self.__ui = Ui_FormTimerSet()
        self.__ui.setupUi(self)

        flags = self.windowFlags()
        self.setWindowFlags(flags & ~Qt.WindowContextHelpButtonHint)

    def GetTimer(self):
        timer = DeviceBulb.Timer()
        #定时器ID
        timer.id = self.__ui.lineEdit_TimerID.text().strip()
        #定时器启用
        timer.enable = self.__ui.checkBox_TimerEnable.isChecked()
        #开始时间
        timer.start = self.__ui.timeEdit_TimerStartTime.text().strip()
        #结束时间
        timer.end = self.__ui.timeEdit_TimerStopTime.text().strip() if self.__ui.checkBox_TimerStopTime.isChecked() else "";

        services = []
        #灯开关
        switch_on = DeviceBulb.ServiceParams("switch", "on", self.__ui.checkBox_switch.isChecked())
        services.append(switch_on)
        if self.__ui.checkBox_switch.isChecked():
            #灯光模式
            lightmode = DeviceBulb.ServiceParams("lightMode", "status", self.__ui.comboBox_LightMode.currentIndex())
            services.append(lightmode)
            #服务配置项
            if DeviceBulb.LightMode.LIGHT_RGB == lightmode.paraValue:
                services.append(DeviceBulb.ServiceParams("rgbBrightness", "brightness", self.__ui.horizontalSlider_Timer_rgbBrightness.value()))
                services.append(DeviceBulb.ServiceParams("saturation", "ratio", self.__ui.horizontalSlider_Timer_Saturation.value()))
                services.append(DeviceBulb.ServiceParams("colour", "red", self.__ui.horizontalSlider_Timer_Red.value()))
                services.append(DeviceBulb.ServiceParams("colour", "green", self.__ui.horizontalSlider_Timer_Green.value()))
                services.append(DeviceBulb.ServiceParams("colour", "blue", self.__ui.horizontalSlider_Timer_Blue.value()))
            else:
                services.append(DeviceBulb.ServiceParams("whiteBrightness", "brightness", self.__ui.horizontalSlider_Timer_whiteBrightness.value()))
                services.append(DeviceBulb.ServiceParams("colorTemperature", "ratio", self.__ui.horizontalSlider_Timer_Temprature.value()))
        timer.services = services

        #执行周期
        if self.__ui.checkBox_NoRepeat.isChecked():
            timer.week = 128
        else:
            if self.__ui.checkBox_Mon.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_MON)
            if self.__ui.checkBox_Tue.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_TUE)
            if self.__ui.checkBox_Wed.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_WED)
            if self.__ui.checkBox_Thu.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_THU)
            if self.__ui.checkBox_Fri.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_FRI)
            if self.__ui.checkBox_Sat.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_SAT)
            if self.__ui.checkBox_Sun.isChecked():
                timer.week |= int(DeviceBulb.DeviceWeak.WEAK_BIT_SUN)

        return timer
		
    def SetTimer(self, timer):
        #定时器ID
        self.__ui.lineEdit_TimerID.setText(timer.id)
        #定时器启用
        self.__ui.checkBox_TimerEnable.setChecked(timer.enable)
        #开始时间
        self.__ui.timeEdit_TimerStartTime.setTime(QTime.fromString(timer.start))
        #结束时间
        self.__ui.checkBox_TimerStopTime.setChecked(False if (timer.end == "") else True);
        self.__ui.timeEdit_TimerStopTime.setTime(QTime.fromString("" if (timer.end == "") else timer.end))

        #灯光模式
        for i in range(len(timer.services)):
            if "switch" == timer.services[i].serviceId:
                self.__ui.checkBox_switch.setChecked(1 == timer.services[i].paraValue)
            elif "lightMode" == timer.services[i].serviceId:
                self.__ui.comboBox_LightMode.setCurrentIndex(timer.services[i].paraValue)
            elif "rgbBrightness" == timer.services[i].serviceId:
                self.__ui.horizontalSlider_Timer_rgbBrightness.setValue(timer.services[i].paraValue)
            elif "saturation" == timer.services[i].serviceId:
                self.__ui.horizontalSlider_Timer_Saturation.setValue(timer.services[i].paraValue)
            elif "colour" == timer.services[i].serviceId:
                if "red" == timer.services[i].para:
                    self.__ui.horizontalSlider_Timer_Red.setValue(timer.services[i].paraValue)
                elif "green" == timer.services[i].para:
                    self.__ui.horizontalSlider_Timer_Green.setValue(timer.services[i].paraValue)
                else:
                    self.__ui.horizontalSlider_Timer_Blue.setValue(timer.services[i].paraValue)
            elif "whiteBrightness" == timer.services[i].serviceId:
                self.__ui.horizontalSlider_Timer_whiteBrightness.setValue(timer.services[i].paraValue)
            elif "colorTemperature" == timer.services[i].serviceId:
                self.__ui.horizontalSlider_Timer_Temprature.setValue(BulbParser.TemperatureRate(timer.services[i].paraValue))
            else:
                MessageBoxFunc.ShowMessageBox("编辑定时器","未知的服务ID:%s" % (timer.services[i].serviceId), self, QMessageBox.Critical)

        #执行周期
        if timer.week > 127:
            self.__ui.checkBox_NoRepeat.setChecked(True)
        else:
            self.__ui.checkBox_Mon.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_MON))
            self.__ui.checkBox_Tue.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_TUE))
            self.__ui.checkBox_Wed.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_WED))
            self.__ui.checkBox_Thu.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_THU))
            self.__ui.checkBox_Fri.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_FRI))
            self.__ui.checkBox_Sat.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_SAT))
            self.__ui.checkBox_Sun.setChecked(timer.week & int(DeviceBulb.DeviceWeak.WEAK_BIT_SUN))

    @Slot()
    def on_okButton_clicked(self):
        checked = self.__ui.checkBox_Mon.isChecked() or self.__ui.checkBox_Tue.isChecked() or self.__ui.checkBox_Wed.isChecked() or self.__ui.checkBox_Thu.isChecked() or self.__ui.checkBox_Fri.isChecked() or self.__ui.checkBox_Sat.isChecked() or self.__ui.checkBox_Sun.isChecked() or self.__ui.checkBox_NoRepeat.isChecked()

        if not checked:
            self.SetRepeatStyleSheet("color:red;")
            MessageBoxFunc.ShowMessageBox("定时器设置","请选择一个执行周期!", self, QMessageBox.Warning)
            return

        self.accept()
        
    @Slot()
    def on_cancelButton_clicked(self):
        self.reject()
        
    @Slot(int)
    def on_checkBox_switch_stateChanged(self, state):
        self.__ui.comboBox_LightMode.setEnabled(state == Qt.CheckState.Checked.value)
        self.__ui.groupBox_RGB.setEnabled(self.__ui.comboBox_LightMode.currentIndex() == 0 and state == Qt.CheckState.Checked.value)
        self.__ui.groupBox_White.setEnabled(self.__ui.comboBox_LightMode.currentIndex() == 1 and state == Qt.CheckState.Checked.value)

    @Slot(int)
    def on_checkBox_TimerStopTime_stateChanged(self, state):
        self.__ui.timeEdit_TimerStopTime.setEnabled(state == Qt.CheckState.Checked.value)
        
    @Slot(int)
    def on_comboBox_LightMode_currentIndexChanged(self, index):
        self.__ui.groupBox_RGB.setEnabled(self.__ui.checkBox_switch.isChecked() and 0 == index)
        self.__ui.groupBox_White.setEnabled(self.__ui.checkBox_switch.isChecked() and 1 == index)

    @Slot(int)
    def on_checkBox_Mon_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")

    @Slot(int)
    def on_checkBox_Tue_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")
            
    @Slot(int)
    def on_checkBox_Wed_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")
            
    @Slot(int)
    def on_checkBox_Thu_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")
            
    @Slot(int)
    def on_checkBox_Fri_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")
            
    @Slot(int)
    def on_checkBox_Sat_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")
            
    @Slot(int)
    def on_checkBox_Sun_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_NoRepeat.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")
            
    @Slot(int)
    def on_checkBox_NoRepeat_stateChanged(self, state):
        if Qt.CheckState.Checked.value == state:
            self.__ui.checkBox_Mon.setChecked(False)
            self.__ui.checkBox_Tue.setChecked(False)
            self.__ui.checkBox_Wed.setChecked(False)
            self.__ui.checkBox_Thu.setChecked(False)
            self.__ui.checkBox_Fri.setChecked(False)
            self.__ui.checkBox_Sat.setChecked(False)
            self.__ui.checkBox_Sun.setChecked(False)
            self.SetRepeatStyleSheet("color:black;")

    def SetRepeatStyleSheet(self, styleSheet):
        self.__ui.label_Repeat.setStyleSheet(styleSheet)
        self.__ui.checkBox_Mon.setStyleSheet(styleSheet)
        self.__ui.checkBox_Tue.setStyleSheet(styleSheet)
        self.__ui.checkBox_Wed.setStyleSheet(styleSheet)
        self.__ui.checkBox_Thu.setStyleSheet(styleSheet)
        self.__ui.checkBox_Fri.setStyleSheet(styleSheet)
        self.__ui.checkBox_Sat.setStyleSheet(styleSheet)
        self.__ui.checkBox_Sun.setStyleSheet(styleSheet)
        self.__ui.checkBox_NoRepeat.setStyleSheet(styleSheet)

